package com.example.myapplication.provider;

import com.example.myapplication.ResourceTable;
import com.example.myapplication.beans.ProductImg;
import com.example.myapplication.utils.ApiUtil;
import com.example.myapplication.utils.LoadNetImageUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public class ProductImagePageSliderProvider extends PageSliderProvider {

    private List<ProductImg> list;
    private AbilitySlice abilitySlice;

    public ProductImagePageSliderProvider(List<ProductImg> list, AbilitySlice abilitySlice) {
        this.list = list;
        this.abilitySlice = abilitySlice;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        //获取数据
        ProductImg productImg = list.get(i);
        //获取模板
        Component template = LayoutScatter.getInstance(abilitySlice).parse(ResourceTable.Layout_index_image_template, null, false);
        Image image = template.findComponentById(ResourceTable.Id_index_image);
        //渲染模板
        String url = ApiUtil.BASE_PATH + productImg.getUrl();
        LoadNetImageUtil.loadImg(abilitySlice,url,image);
        //载入pageSlider ---->componentContainer
        componentContainer.addComponent(template);
        return template;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent((Component) o);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return true;
    }
}
